package com.controller;

import com.annotation.Logging;
import com.bean.ActJoinInfo;
import com.bean.ActPic;
import com.bean.Activity;
import com.bean.vo.ActivityVo;
import com.service.ActivityService;
import com.utils.AjaxResult;
import com.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;



   private    String staticPath=
//           new ApplicationHome(this.getClass()).getDir()
//            .getParentFile().getParentFile().getAbsolutePath()
//            +"\\src\\main\\resources\\image\\"  ;
"D:\\试炼小项目作业\\汉族日历后端\\han-back\\src\\main\\resources";

    private Logger logger= LoggerFactory.getLogger(ActivityController.class);

    //图片上传
    @RequestMapping( value="/testUpload")
    //@Logging
    public AjaxResult uploadPic( @RequestParam(value="file",required=false) MultipartFile file){
        logger.info(file.getName());
        //非空
        if(file.isEmpty()){
            return AjaxResult.error();
        }
        //重命名
        String originalFilename  =file.getOriginalFilename();
        String houzhui="."+ originalFilename.split("\\.")[1];
        String newname=  UUID.randomUUID().toString()+houzhui;
        //放置到路径之下
        ApplicationHome applicationHome=
                new ApplicationHome(this.getClass());
        //先是获取到target then get the project
        String path=this.staticPath+"\\image\\";
//                applicationHome.getDir()
//                .getParentFile().getParentFile().getAbsolutePath()
//             +"\\src\\main\\resources\\image\\"  ;
        String truePath=path+newname;//绝对路径也

        try {
            file.transferTo(new File(truePath));
        } catch (IOException e) {
            logger.info("图片上传异常",e);
            return AjaxResult.error();
        }
        logger.info("上传成功",newname);
        return AjaxResult.success("上传成功",newname);
    }

    //真实的图片查看
   @GetMapping("/downLoad")
    public void downLoad(String name, HttpServletResponse response){
       try {
           //输入流读取文件内容
           FileInputStream fileInputStream=
                   new FileInputStream(new File(this.staticPath+"\\image\\"+name));
          //输出流 展示
           try {
               ServletOutputStream outputStream=
                       response.getOutputStream();

               response.setContentType("image/jpg");
               int len=0;
               byte[] bytes=new byte[1024];
               while((len=fileInputStream.read(bytes))!=-1){
                   outputStream.write(bytes,0,len);
                   outputStream.flush();
               }
               outputStream.close();
               fileInputStream.close();
           } catch (IOException e) {
               e.printStackTrace();
           }


       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }

   }

    //单个图片展示
    @RequestMapping("/showPic")
    @Logging
    public AjaxResult showPic(@PathVariable String image) throws MalformedURLException {
        File file=new File(staticPath+image);
//        try {

            Resource resource=new UrlResource(file.toURI());
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }

        return AjaxResult.success("加载成功",resource);
    }

    //视频上传
    public AjaxResult uploadVedio(){
        return null;
    }

    //音乐上传
    public AjaxResult uploadMp3(){
        return null;
    }
    //文件上传
    public AjaxResult uploadOther(){
        return null;
    }

    //查看活动列表
    @RequestMapping("/checkActivityList")
    public AjaxResult checkActivityList(){

      List<ActivityVo>activityVoList=  activityService.selectActivityList(null);

      return AjaxResult.success(activityVoList);

    }

    //查看活动详情以及报名人数
    @RequestMapping("/checkActivityList")
    @Logging
    public AjaxResult checkActivityDetail(@RequestBody Activity a){

        //查活动
      ActivityVo activityVo= activityService.checkOneActivity(a);
      if(StringUtils.isNull(activityVo)){
          return AjaxResult.error();
      }
        return AjaxResult.success(activityVo);

    }
    //报名参加活动
    @RequestMapping("/joinActivity")
    @Logging
    public AjaxResult joinActivity(@RequestBody ActJoinInfo actJoinInfo){
        actJoinInfo.setIsSign("0");
        activityService.joinActivity(actJoinInfo);
        return AjaxResult.success();
    }

    //添加活动
    @RequestMapping("/addActivity")
    @Logging
    public AjaxResult addActivity(@RequestBody ActivityVo a){

        ActPic actPic=new ActPic();
        actPic.setPicInfo(a.getPicInfo());
        Activity activity=new Activity();
        BeanUtils.copyProperties(a,activity);
        activity.setIsMarked("0");
        //添加活动
        int i= activityService.addActivity(activity,actPic);
        if(i!=1){
            return AjaxResult.error();
        }
        return AjaxResult.success();

    }

    //活动签到
    @RequestMapping("/addActivity")
    @Logging
    public AjaxResult addActivity(@RequestBody ActJoinInfo a){


        return AjaxResult.success();

    }
    //在service中写定时任务，把报名但是没参加活动的人，放进放鸽子名单中



}
